<template>
	<div class="container">
		<el-dialog :title="title + '帖子'" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
			<el-form ref="form" :model="form" label-width="120px">
				<el-form-item label="帖子名称">
					<el-input v-model="form.name"></el-input>
				</el-form-item>
				<el-form-item label="帖子标题">
					<el-input v-model="form.title"></el-input>
				</el-form-item>
				<el-form-item label="帖子内容">
					<el-input show-password v-model="form.content" type="textarea" :rows="10"></el-input>
				</el-form-item>
				<el-form-item label="帖子喜欢数">
					<el-input v-model="form.like" type="number"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
				<el-button type="primary" @click="onSubmit">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import * as postApi from "@/api/post.js"; //引入
	export default {
		data() {
			return {
				dialogVisible: false,
				form: {
					id: "",
					name: "",
					title: "",
					content: "",
				},
				type: 'add'
			}
		},
		computed: {
			title() {
				return this.type == 'add' ? '添加' : "编辑"
			}
		},
		methods: {
			open(row, type) {
				this.dialogVisible = true
				this.type = type
				if (type == 'edit') {
					this.form = row
				} else {
					this.form = {
						id: "",
						name: "",
						title: "",
						content: "",
					}
				}
			},
			handleClose() {
				this.dialogVisible = false
			},
			onSubmit() {
				let method = this.type == 'add' ? 'add' : 'update'
				postApi[method](this.form).then(res => {
					if (res.code == 200) {
						this.$message.success(res.message)
						this.handleClose()
						this.$emit("close")
					}
				})
			}
		}
	}
</script>

<style>
</style>